# crawler/serializer.py

from rest_framework import serializers

from .models import City,CityData,CityReview

import pandas as pd


class CitySerializer(serializers.ModelSerializer):
	class Meta:
		model = City
		fields = '__all__'


class CityDataSerializer(serializers.ModelSerializer):
	city = serializers.SerializerMethodField()
	publish_time = serializers.SerializerMethodField()

	def get_city(self, obj):
		return obj.city.name

	def get_publish_time(self, obj):
		publish_time = pd.to_datetime(obj.publish_time)
		obj.publish_time = publish_time.strftime("%Y-%m-%d")
		return obj.publish_time

	class Meta:
		model = CityData
		fields = ('id','publish_time','week','top_temperature','low_temperature','weather','wind_direction','city')


class CityReviewSerializer(serializers.ModelSerializer):
	class Meta:
		model = CityReview
		fields = '__all__'